package com.lishem.carl._05doublepointer;

import com.lishem.common.ListNode;

/**
 * https://leetcode.cn/problems/reverse-linked-list/description/
 * <p>
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 * <p>
 * 示例 1：
 * <p>
 * 输入：head = [1,2,3,4,5]
 * <p>
 * 输出：[5,4,3,2,1]
 * <p>
 * 示例 2：
 * <p>
 * 输入：head = [1,2]
 * <p>
 * 输出：[2,1]
 * <p>
 * 示例 3：
 * <p>
 * 输入：head = []
 * <p>
 * 输出：[]
 */
public class _9LetCode206_反转链表 {

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        ListNode next;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        _9LetCode206_反转链表 sol = new _9LetCode206_反转链表();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        ListNode.printListNode(head);
        ListNode.printListNode(sol.reverseList(head));
    }
}
